package com.eshipenet.shipnet.equipmentextbuilder

import scala.util.Try

sealed trait EquipmentModelLinkAction {
  val eid: String
  val mid: String
  val raw: DebeziumParser.Action
  val actionInfo: DebeziumActionInfo = DebeziumActionInfo(raw)
}

case class EquipmentModelLinkInsert(after: EquipmentModelLink, raw: DebeziumParser.Insert)
    extends EquipmentModelLinkAction {
  override val eid: String = after.eid
  override val mid: String = after.mid
}
case class EquipmentModelLinkUpdate(before: EquipmentModelLink, after: EquipmentModelLink, raw: DebeziumParser.Update)
    extends EquipmentModelLinkAction {
  override val eid: String = after.eid
  override val mid: String = after.mid
}
case class EquipmentModelLinkDelete(before: EquipmentModelLink, raw: DebeziumParser.Delete)
    extends EquipmentModelLinkAction {
  override val eid: String = before.eid
  override val mid: String = before.mid
}

object EquipmentModelLinkAction {

  def apply(act: DebeziumParser.Action): Try[EquipmentModelLinkAction] = act match {
    case insert @ DebeziumParser.Insert(after, _) =>
      EquipmentModelLink(after).map(e => EquipmentModelLinkInsert(e, insert))
    case update @ DebeziumParser.Update(before, after, _) =>
      Try {
        val boxBefore = EquipmentModelLink(before).get
        val boxAfter  = EquipmentModelLink(after).get
        EquipmentModelLinkUpdate(boxBefore, boxAfter, update)
      }
    case delete @ DebeziumParser.Delete(before, _) =>
      EquipmentModelLink(before).map(e => EquipmentModelLinkDelete(e, delete))
  }

}
